import Api from '@/services/api.js';
import { checkPhoneNumber } from '@/utils/utils';
const app = getApp();
export default {
  components: {
  },
  data() {
    return {
      department: {}, // 部门信息
      PRIMARY_TEXT_COLOR: app.globalData.PRIMARY_TEXT_COLOR,
      info: {
        // job: '',
        // jobId: '',
        // name: '',
        phone: '',
        pwd: ''
      },
      posts: [{
        key: '1',
        val: '总监'
      },{
        key: '2',
        val: '专员'
      }],
    }
  },
  computed: {
    // 每一项填完之后才可以保存
    canSave() {
      return Object.keys(this.info).find(key => !this.info[key]);
    },
    privacyData() {
      return this.$store.state.privacyData;
    },
    companyInfo() {
      return this.$store.state.privacyData;
    },
  },
  onLoad(options) {
    this.department = JSON.parse(options.department);
    uni.setNavigationBarTitle({
      title: this.department.name
    })
  },
  mounted() {
    // this.getEnterpriseDepartments()
  },
  methods: {
    // 根据企业id获取企业部门
    // async getEnterpriseDepartments() {
    //   this.departments = await this.$Serv.getEnterpriseDepartments();
    // },
    // handlePost() {
    //   this.$refs.commonPickerPop.togglePopup(true, { arr: this.posts, value: this.info.jobId })
    // },
    handleBtnTap() {
      if (this.info.id) {
        this.$refs.noticeModal.toggleModal(true);
      }else {
        uni.navigateBack();
      }
      
    },
    // 删除事件
    handleDelete() {

    },
    handleSave() {
      // if (!this.info.job) {
      //   return uni.showToast({ title: '请选择岗位', icon: 'none' });
      // }
      // if (!this.info.name) {
      //   return uni.showToast({ title: '请输入姓名', icon: 'none' });
      // }
      if (!this.info.phone) {
        return uni.showToast({ title: '请输入联系电话', icon: 'none' });
      }
      if (!checkPhoneNumber(this.info.phone)) {
        return uni.showToast({ title: '请输入正确的联系电话', icon: 'none' });
      }
      if (!this.info.pwd) {
        return uni.showToast({ title: '请输入密码', icon: 'none' });
      }
      // 接口请求
      this.requestEdit();
    },
    async requestEdit() {
      
      const { code, result } = await this.$request({
        url: Api.addHrAdminJobUser,
        method: 'POST',
        data: {
          ...this.info,
          enterpriseId: this.companyInfo.enterpriseId,
          departmentId: this.department.id
        }
      })
      if (code === 200) {
        uni.showToast({ title: '操作成功', icon: 'none' });
        setTimeout(() => {
          uni.navigateBack()
        }, 500)
      }
    },
    onConfirm(e) {
      this.info.job = e.val;
      this.info.jobId = e.key;
    }
  }
}